<?php
require_once("myheader.php");
require_once("myfooter.php");
require_once("helper/connexion.php");
require_once("helper/enseignement_helper.php");
require_once("helper/enseignant_helper.php");
myHeader("enseignements");
connexion_sql();

if(isset($_GET['id'])) {
		$id = (int) $_GET['id'];
		$o = (int) $_GET['o'];
		$f = (int) $_GET['f'];
}

if(isset($_GET['delete']) && isset($_GET['date'])) {			
	if($_GET['delete'] == 'td') {
		$date = pg_escape_string($_GET['date']);
		supprTd($_SESSION['id'],$_SESSION['id_enseignement_detail'],$date);
	}
	
	if($_GET['delete'] == 'cm') {
		$date = pg_escape_string($_GET['date']);
		supprCoursMagist($_SESSION['id'],$_SESSION['id_enseignement_detail'], $date);
	}
	$id = $_SESSION['id_enseignement_detail'];
	$o = $_SESSION['o_enseignement_detail'];
	$f = $_SESSION['f_enseignement_detail'];
	
	unset($_SESSION['id_enseignement_detail']);	
	unset($_SESSION['o_enseignement_detail']);	
	unset($_SESSION['f_enseignement_detail']);	
	
}

if(isset($_POST) && !empty($_POST)) {
	if($_POST['heuresCours'] > 0) {
		$heuresCours = (float) $_POST['heuresCours'];
		@ajoutCoursMagist($_SESSION['id'], $_SESSION['id_enseignement_detail'],$heuresCours);
	}
	
	
	if($_POST['groupes'] > 0 && $_POST['heuresGroupesTD'] > 0) {
		$groupes = (int) $_POST['groupes'];
		$heuresGroupesTD = (float) $_POST['heuresGroupesTD'];	
		@ajoutTd($_SESSION['id'], $_SESSION['id_enseignement_detail'],$groupes , $heuresGroupesTD);
    }
    
    unset($_SESSION['id_enseignement_detail']);
}
$data_enseignement = getEnseignementById($id);
$fetchEns = pg_fetch_assoc($data_enseignement);
?>
<div class="container">
<div id="body_detail">
<h2>Details des Enseignements</h2>
<br>
<h3>Cours Magistraux (<?php echo $fetchEns['heurecm']; ?>h)</h3>
<table class="table table-striped">
			<thead>
				<tr>
					<th>Enseignant</th>
					<th>Date voeux</th>
					<th>Nombre heures</th>
					<?php echo (isset($_SESSION['id'])) ? '<th>Actions</th>' : ''; ?>
				</tr>
			</thead>
			<tbody>         
			  <?php
			  $req2 = getCours($id,$o,$f);
			  while($rowOption = pg_fetch_assoc($req2)){				  
			  ?> 
			    <tr>
					<td><?php echo $rowOption['nomens']; ?>  <?php echo $rowOption['prenomens']; ?></td>
					<td><?php echo $rowOption['choix']; ?></td>
					<td><?php echo $rowOption['heure']; ?></td>
					<?php 
						$content = (isset($_SESSION['id']) && $rowOption['id'] == $_SESSION['id']) ? '<a href="?delete=cm&date='.$rowOption['choix'].'"><i class="icon-remove"></i></a>' : '';
						echo (isset($_SESSION['id'])) ? '<td>'.$content.'</td>':'';	?>
				</tr>
			<?php } ?>			
			</tbody>
			</table>

	
	</br>

<h3>Travaux Dirigés (<?php echo $fetchEns['heuretd']; ?>h)</h3>		
			

				<table class="table table-striped">
							   
							   
							    <thead>
								<tr>
								<th>Enseignant</th>
								<th>Date voeux</th>
								<th>Nombre heures/Groupes</th>
								<?php echo (isset($_SESSION['id'])) ? '<th>Actions</th>' : ''; ?>
								</tr>
								</thead>
								<tbody>	
			
			  <?php
			  
			  
								
								  $req2 = getTD($id,$o,$f);
								  
								  while($rowOption = pg_fetch_assoc($req2)){									  
								  ?> 
							    <tr>
								<td><?php echo $rowOption['nomens']; ?>  <?php echo $rowOption['prenomens']; ?></td>
								<td><?php echo $rowOption['choix']; ?></td>
								<td><?php echo $rowOption['heure']; ?></td>
								<?php 
						$content = ($rowOption['id'] == $_SESSION['id']) ? '<a href="?delete=td&date='.$rowOption['choix'].'"><i class="icon-remove"></i></a>' : '';
						echo (isset($_SESSION['id'])) ? '<td>'.$content.'</td>':'';	?>
								
								</tr>	
								
								<?php } ?>
											</tbody>
							   

						   </table>
			
			
				

			<?php
				if(isset($_SESSION['id'])) {
					$_SESSION['id_enseignement_detail'] = $id;
					$_SESSION['o_enseignement_detail'] = $o;
					$_SESSION['f_enseignement_detail'] = $f;
			?>
			    <form class="form-inline" method="post">
					<input type="text" class="input-small" placeholder="heures" id="heuresCours" name="heuresCours"> <label class="control-label" for="heuresCours">Cours</label><br><br>
					<input type="text" class="input-small" placeholder="groupes" name="groupes" id="groupes"> 
					<input type="text" class="input-small" placeholder="heures/groupe" id="heuresGroupesTD" name="heuresGroupesTD"> <label class="control-label" for="heuresGroupesTD">TD</label><br><br>
					
					<button type="submit" class="btn">Modifier</button>
				</form>
			<?php
				}
			?>


	</div>
</div>

<?php
	myFooter();
?>

